package com.wwwarehouse.resource_center.fragment.rules;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.rules.TaskDetailedRule;

import java.util.ArrayList;

/**
 * @author Joseph on 2017/8/23.
 *         <p/>
 */

public class SelectExistingRulesListFragment extends TheParentFragment implements DialogTools.ConfirmListener {

    private ListView lvlist;
    private ArrayList<TaskDetailedRule> mAllData ;
    private ArrayList<TaskDetailedRule> mGridData;
    private ExistingRulesListAdaper mAdapter ;
    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_edit_task_list;
    }
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
    @Override
    public void onSuccess(CommonClass commonClass, int i) {

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    protected void init(View view) {
        lvlist = findView(view,R.id.lv_list);
    }

    @Override
    protected void loadDatas() {
//        Bundle b = getArguments();
//        mAllData =  b.getParcelableArrayList("data");
//        mGridData = new ArrayList<>();
//        int pos = b.getInt("pos");
//        int num = b.getInt("num");
//        for (int i = pos * num; i < num + pos * num; i++) {
//            if (i >= 0 && i < mAllData.size()) {
//                mGridData.add(mAllData.get(i));
//            }
//        }
//        mAdapter = new ExistingRulesListAdaper();
//        lvlist.setAdapter(mAdapter);
    }

    @Override
    public void setConfirmListener(Dialog dialog, View view, String input) {
        //完成，配置成功
        pushFragment(new ConfigurationSuccessFragment());

        dialog.dismiss();
    }

    private class ExistingRulesListAdaper extends BaseAdapter{

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
           ViewHodler viewHodler;
            if (convertView == null) {
                viewHodler = new ViewHodler();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_rule_exist_rule_list_item, parent, false);
                ViewTreeObserver vto = lvlist.getViewTreeObserver();
                final View finalConvertView = convertView;
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
                    @Override
                    public void onGlobalLayout() {
                        lvlist.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        int mItemHeight = lvlist.getHeight() / 11;
                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        layoutParams.height = mItemHeight ;
                        finalConvertView.setLayoutParams(layoutParams);
                    }
                });
                viewHodler.mRuleName = (TextView) convertView.findViewById(R.id.rule_name);
                viewHodler.mBtnDetails = (Button) convertView.findViewById(R.id.bt_details);
                viewHodler.mRuleName.setText(mGridData.get(position).getRuleName());
                convertView.setTag(viewHodler);
            } else {
                viewHodler = (ViewHodler) convertView.getTag();
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //确认添加,dialog
                    DialogTools.getInstance().showCustomDialogPrompt(mActivity, "确认添加", "确认添加任务"+""+"到"+""+"中吗？", false, SelectExistingRulesListFragment.this);
                }
            });
//            viewHodler.mBtnDetails.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    //跳转到详情页
//                    SetRulesTaskCardDetailsFragment detailsFragment = new SetRulesTaskCardDetailsFragment();
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("data", mGridData.get(position));
//                    detailsFragment.setArguments(bundle);
//                    pushFragment(detailsFragment);
//                }
//            });

            return convertView;
        }
        @Override
        public int getCount() {
            return mGridData.size();
        }

        @Override
        public Object getItem(int position) {
            return mGridData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }
    class ViewHodler{
        TextView mRuleName;
        Button mBtnDetails;
    }
}
